package com.tang.algorithm.doublePointer;

/**
 * @author tlm
 * @Description 环形链表2
 * @date 2020/7/21 2:47 下午
 */
public class LeetCode142 {
	public ListNode detectCycle(ListNode head) {
		ListNode slow = head;
		ListNode fast = head;
		while (true) {
			if (fast == null || fast.next == null) {
				return null;
			}
			slow = slow.next;
			fast = fast.next.next;
			if (fast == slow)
				break;
		}
		fast = head;
		while (fast != slow) {
			fast = fast.next;
			slow = slow.next;
		}

		return fast;
	}
}
